"""
common function for other module to use
"""
import re

def getLineIndexBeginWith(lines,thestr,beginindex=0):
    """
    get the line index of the first one that begin with thestr
    if not exist, return -1
    """
    for i in range(beginindex,len(lines)):
        if lines[i].startswith(thestr):
            return i
    return -1


def getLineHas(lines,thestr,beginindex=0):
    """
    get the first line has the str
    """
    curindex = beginindex
    while curindex < len(lines):
        #if len(re.findall(thestr,lines[curindex])) != 0:
        if lines[curindex].find(thestr) == -1:
            curindex += 1
        else:
            return curindex
    return -1
        
    
def getLineItems(thestr,thesplit=" "):
    """
    the split str of the line
    and remove all empty strings
    """
    thestr = removeNewLineChar(thestr)
    items = thestr.split(thesplit)
    result = []
    for i in items:
        if i != "":
            result.append(i)
    return result
    
def removeNewLineChar(s):
    """
    some line end withs \n
    """
    if s.endswith("\n"):
        return s[0:len(s)-1]
    return s
